package com.mobilebook.properties;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class PropertiesProxy implements ApplicationContextAware{
	private static ApplicationContext ac = null;
	private static GlobalPropertyPlaceholderConfigurer gppc = null;
	@Override
	public void setApplicationContext(ApplicationContext actx)
			throws BeansException {
		if (ac != null) {
			return;
		}
		synchronized (PropertiesProxy.class) {
			if (ac != null) {
				return;
			}
			ac = actx;
		}
		
	}

	public static String getPropertyString(String key){
		if(gppc == null){
			gppc = (GlobalPropertyPlaceholderConfigurer) ac.getBean("propertyConfigurer");
		}
		return gppc.getProperty(key);
	}
	
	public static int getPropertyInt(String key){
		String v = getPropertyString(key);
		int i = Integer.parseInt(v);
		return i;
	}
	
	public static int getPropertyInt(String key,int defaultValue){
		String v = getPropertyString(key);
		if(v == null || v.equals(""))
			return defaultValue;
		int i = Integer.parseInt(v);
		return i;
	}
}
